package com.example.decoder;

import android.app.Activity;
import android.app.Dialog;
import android.content.ComponentName;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener {

	private EditText binaryEt, decimalEt, hexEt, b64Et, set64XLEt, thirdEt;
	private EditText anyEt;

	private int anyEncoder = 0;

	private TextView b64TView, anyTextView;

	private Button set64btn;

	private SharedPreferences sp;

	private String XL64 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-_";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		binaryEt = (EditText) findViewById(R.id.binaryET);
		decimalEt = (EditText) findViewById(R.id.decET);
		hexEt = (EditText) findViewById(R.id.hexET);
		b64Et = (EditText) findViewById(R.id.b64ET);
		thirdEt = (EditText) findViewById(R.id.thirdET);
		anyEt = (EditText) findViewById(R.id.anyET);

		binaryEt.addTextChangedListener(new MyTextWather(0));
		decimalEt.addTextChangedListener(new MyTextWather(1));
		hexEt.addTextChangedListener(new MyTextWather(2));
		b64Et.addTextChangedListener(new MyTextWather(3));
		thirdEt.addTextChangedListener(new MyTextWather(4));
		anyEt.addTextChangedListener(new MyTextWather(5));

		set64XLEt = (EditText) findViewById(R.id.set64xulie);

		set64btn = (Button) findViewById(R.id.set64btn);

		b64TView = (TextView) findViewById(R.id.b64Title);
		b64TView.setOnClickListener(this);
		anyTextView = (TextView) findViewById(R.id.anyTitle);
		anyTextView.setOnClickListener(this);

		sp = getSharedPreferences("main", Context.MODE_PRIVATE);
		if (sp.getBoolean("first", true)) {
			sp.edit().putBoolean("first", false).commit();
			// 第一次我们就保存
			sp.edit().putString("XL64", XL64);
		} else {
			XL64 = sp.getString("XL64", XL64);
		}
		anyEncoder = sp.getInt("any_encode", 0);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.menu, menu);
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		set64XLEt.setVisibility(View.GONE);
		set64btn.setVisibility(View.GONE);
		if (item.getItemId() == R.id.action_reset) {
			binaryEt.setText("");
			decimalEt.setText("");
			b64Et.setText("");
			hexEt.setText("");
			thirdEt.setText("");
			anyEt.setText("");
			b64Et.setBackgroundColor(0x33aaaaaa);
			binaryEt.setBackgroundColor(0x33aaaaaa);
			decimalEt.setBackgroundColor(0x33aaaaaa);
			hexEt.setBackgroundColor(0x33aaaaaa);
			thirdEt.setBackgroundColor(0x33aaaaaa);
			anyEt.setBackgroundColor(0x33aaaaaa);

		} else if (item.getItemId() == R.id.action_search) {
			convert(mLastEditPos);
		}
		return super.onOptionsItemSelected(item);
	}

	private void convert(int id) {

		switch (id) {
		case 0: // 2jinzhi
		{
			String b = binaryEt.getText().toString();

			String hex = Convert.BToH(b);

			hexEt.setText(hex);

			String dec = Convert.toD(b, 2);
			decimalEt.setText(dec);

			String b64 = Convert._10to64(Long.parseLong(dec), XL64);
			b64Et.setText(b64);

			String third = Convert._10to3(Long.parseLong(dec));
			thirdEt.setText(third);

			if (anyEncoder > 0) {
				String any = Convert._10toAny(dec, anyEncoder);
				anyEt.setText(any);
			}
			// 下面的代码这么做的原因是因为onTextChanged的原因
			binaryEt.setText(b);
			break;
		}
		case 1: // 10进制修改了
		{
			String dec = decimalEt.getText().toString();
			String hex = Convert._10to16(Long.parseLong(dec));
			hexEt.setText(hex);

			String b64 = Convert._10to64(Long.parseLong(dec), XL64);
			b64Et.setText(b64);

			String bin = Convert.HToB(hex);
			binaryEt.setText(bin);

			String third = Convert._10to3(Long.parseLong(dec));
			thirdEt.setText(third);
			// 下面的代码这么做的原因是因为onTextChanged的原因
			decimalEt.setText(dec);
			break;
		}
		case 2: // 16进制修改了
		{
			String hex = hexEt.getText().toString();
			String dec = Convert.toD(hex, 16);
			decimalEt.setText(dec);

			String b64 = Convert._10to64(Long.parseLong(dec), XL64);
			b64Et.setText(b64);

			String bin = Convert.HToB(hex);
			binaryEt.setText(bin);

			String third = Convert._10to3(Long.parseLong(dec));
			thirdEt.setText(third);
			// 下面的代码这么做的原因是因为onTextChanged的原因
			hexEt.setText(hex);
			break;
		}

		case 3: // 64进制修改了
		{
			String b64 = b64Et.getText().toString();
			long dec = Convert._64to10(b64, XL64); // TODO
			decimalEt.setText("" + dec);

			String hex = Convert._10to16(dec);
			hexEt.setText(hex);

			String bin = Convert.HToB(hex);
			binaryEt.setText(bin);

			String third = Convert._10to3(dec);
			thirdEt.setText(third);
			// 下面的代码这么做的原因是因为onTextChanged的原因
			b64Et.setText(b64);
			break;
		}
		case 4: {
			String third = thirdEt.getText().toString();

			String dec = Convert.toD(third, 3);
			decimalEt.setText("" + dec);

			String hex = Convert._10to16(Long.parseLong(dec));
			hexEt.setText(hex);

			String bin = Convert.HToB(hex);
			binaryEt.setText(bin);

			String b64 = Convert._10to64(Long.parseLong(dec), XL64);
			thirdEt.setText(third);
			// 下面的代码这么做的原因是因为onTextChanged的原因
			b64Et.setText(b64);
			break;
		}
		default:
			break;
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.b64Title:
			set64XLEt.setVisibility(View.VISIBLE);
			set64XLEt.setText(XL64);

			set64btn.setVisibility(View.VISIBLE);
			break;
		case R.id.set64btn:
			sp.edit().putString("XL64", set64XLEt.getText().toString())
					.commit();
		default:
			break;
		}
	}

	private int mLastEditPos = -1;

	private class MyTextWather implements TextWatcher {
		private int m;

		public MyTextWather(int m) {
			this.m = m;
		}

		@Override
		public void onTextChanged(CharSequence s, int start, int before,
				int count) {
			mLastEditPos = m;
			if (mLastEditPos == 0) {
				binaryEt.setBackgroundColor(0x2faa00ee);
				decimalEt.setBackgroundColor(0x33aaaaaa);
				hexEt.setBackgroundColor(0x33aaaaaa);
				b64Et.setBackgroundColor(0x33aaaaaa);
				thirdEt.setBackgroundColor(0x33aaaaaa);
			}
			if (mLastEditPos == 1) {
				decimalEt.setBackgroundColor(0x2faa00ee);
				binaryEt.setBackgroundColor(0x33aaaaaa);
				hexEt.setBackgroundColor(0x33aaaaaa);
				b64Et.setBackgroundColor(0x33aaaaaa);
				thirdEt.setBackgroundColor(0x33aaaaaa);
			}
			if (mLastEditPos == 2) {
				hexEt.setBackgroundColor(0x2faa00ee);
				binaryEt.setBackgroundColor(0x33aaaaaa);
				decimalEt.setBackgroundColor(0x33aaaaaa);
				b64Et.setBackgroundColor(0x33aaaaaa);
				thirdEt.setBackgroundColor(0x33aaaaaa);
			}
			if (mLastEditPos == 3) {
				b64Et.setBackgroundColor(0x2faa00ee);
				binaryEt.setBackgroundColor(0x33aaaaaa);
				decimalEt.setBackgroundColor(0x33aaaaaa);
				hexEt.setBackgroundColor(0x33aaaaaa);
				thirdEt.setBackgroundColor(0x33aaaaaa);
			}
			if (mLastEditPos == 4) {
				thirdEt.setBackgroundColor(0x2faa00ee);
				binaryEt.setBackgroundColor(0x33aaaaaa);
				decimalEt.setBackgroundColor(0x33aaaaaa);
				hexEt.setBackgroundColor(0x33aaaaaa);
				b64Et.setBackgroundColor(0x33aaaaaa);
			}
		}

		@Override
		public void beforeTextChanged(CharSequence s, int start, int count,
				int after) {
		}

		@Override
		public void afterTextChanged(Editable s) {
		}
	};
}
